//
//  HLHomeStatusesViewModel.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/21.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit

class HLHomeStatusesViewModel: NSObject {
    
    class func loadHomeStatuses(since_id: Int64, max_id: Int64 ,finshed: (statuses: [HLHomeStatuses]?) -> ()) {
        //获取用户的token,并判断token有没有值
        guard let userToken = HLUserAccountViewModel().userToken else {
            //没有值就不进行数据加载
            return
        }
        
        //配置请求参数
        var parameters = ["access_token": userToken]
        //根据传入的参数修改请求参数
        if since_id != 0 {
            parameters["since_id"] = "\(since_id)"
        }else if max_id != 0 {
            parameters["max_id"] = "\(max_id - 1)"
        }
        
        HLNetworking.sharedNetworking.sendHTTPRequest(.GET, urlStr: "2/statuses/home_timeline.json", parameters: parameters) { (resultData, error) -> () in
            //判断请求数据是否成功
            if error == nil {
                //判断状态信息是否有值
                guard let statuses = resultData!["statuses"] as? [[String: AnyObject]] else{
                    return
                }
                //获取状态信息,字典转模型
                //创建可变字典
                var statusesArr = [HLHomeStatuses]()
                //遍历字典数组
                for statusDic in statuses {
                    //字典转模型
                    let status = HLHomeStatuses(dic: statusDic)
                    //将模型数据放入数组中
                    statusesArr.append(status)
                }
                finshed(statuses: statusesArr)
            }
        }
    }
    
}
